package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.OrderModelCalculateHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PublicDMPPlatformOrderModelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liaojing
 * 雁飞格物DMP平台-公有化--订购模式属性联动，包括展示与不展示以及总销售价和总结算价的计算
 * @create 2021-12-31-15:24
 */
@Service
@AttrHandler(PublicDMPPlatformOrderModelAttrHandler.class)
public class PublicDMPPlatformOrderModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();

        //D1档次-(0,9000]结算价格（元）
        KiteAttr LastestPriceAttr_D1 = inst.getAttr("210008074");
        //D1档次-(0,9000]销售价格（元）
        KiteAttr sellAttr_D1 = inst.getAttr("210008075");
        //D2档次-(9000,60000]结算价格（元）
        KiteAttr LastestPriceAttr_D2 = inst.getAttr("210008076");;
        //D2档次-(9000,60000]销售价格（元）
        KiteAttr sellAttr_D2 = inst.getAttr("210008077");
        //D3档次-(60000, 720000]结算价格（元）
        KiteAttr LastestPriceAttr_D3 = inst.getAttr("210008078");
        //D3档次-(60000, 720000]销售价格（元）
        KiteAttr sellAttr_D3 = inst.getAttr("210008079");
        //N1档次-（0-1000]/D1结算价格（万元）
        KiteAttr LastestPriceAttr_N1 = inst.getAttr("210008080");
        //N1档次-（0-1000]/D1销售价格（万元）
        KiteAttr sellAttr_N1 = inst.getAttr("210008081");
        //N2档次-（1000-2000]/D2结算价格（万元）
        KiteAttr LastestPriceAttr_N2 = inst.getAttr("210008082");
        //N2档次-（1000-2000]/D2销售价格（万元）
        KiteAttr sellAttr_N2 = inst.getAttr("210008083");
        //N3档次-（2000-3000]/D3结算价格（万元）
        KiteAttr LastestPriceAttr_N3 = inst.getAttr("210008084");
        //N3档次-（2000-3000]/D3销售价格（万元）
        KiteAttr sellAttr_N3 = inst.getAttr("210008085");
        //订购年限
        KiteAttr year = inst.getAttr("210008134");
        //包年服务总结算价（万元）
        KiteAttr LastestPriceAttr_all = inst.getAttr("210008139");
        //包年服务总销售价（万元）
        KiteAttr sellAttr_all = inst.getAttr("210008140");

        //获取N1结算价格、销售价格
        String lpa_n1 = LastestPriceAttr_N1.getValue();
        String sa_n1 = sellAttr_N1.getValue();
        //获取N2结算价格、销售价格
        String lpa_n2 = LastestPriceAttr_N2.getValue();
        String sa_n2 = sellAttr_N2.getValue();
        //获取N3结算价格、销售价格
        String lpa_n3 = LastestPriceAttr_N3.getValue();
        String sa_n3 = sellAttr_N3.getValue();
        //结算价折扣率
        String discountRate = inst.getAttr("210008073").getValue();
        //订购年限
        String orderYear = year.getValueDesc();

        if (KiteStringUtils.isNotEmpty(attrValue)) {
            //订购模式为按量计费时
            if (KiteStringUtils.isEqual(attrValue, "100000157")) {
                //初始化D1销售价为默认值
                operateInstAttrService.setAttrValue(
                        inst, "210008075", "0.6", data.getSceneInstId(), null);
                //初始化D2销售价为默认值
                operateInstAttrService.setAttrValue(
                        inst, "210008077", "1.2", data.getSceneInstId(), null);
                //初始化D3销售价为默认值
                operateInstAttrService.setAttrValue(
                        inst, "210008079", "2.5", data.getSceneInstId(), null);
                //结算价折扣率清空
                if(!KiteStringUtils.isBlank(discountRate)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008073", null, data.getSceneInstId(), null);
                }
                LastestPriceAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                year.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
            }
            //订购模式为包年服务N1档次
            if (KiteStringUtils.isEqual(attrValue, "110015093")) {
                //结算价折扣率清空
                if(!KiteStringUtils.isBlank(discountRate)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008073", null, data.getSceneInstId(), null);
                }
                //初始化N1销售价为默认值
                operateInstAttrService.setAttrValue(
                        inst, "210008081", "2.5", data.getSceneInstId(), null);
                LastestPriceAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                year.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                year.setIsNull(KeyConsts.IFTRUE_F);
//                //总结算价计算值
//                if(!KiteStringUtils.isEmpty(lpa_n1) && !KiteStringUtils.isEmpty(orderYear)){
//                    String result_lpa_n1 = OrderModelCalculateHelper.getResultLpa(lpa_n1,discountRate,orderYear);
//                    if(KiteStringUtils.isNotEmpty(result_lpa_n1)) {
//                        operateInstAttrService.setAttrValue(
//                                inst, "210008139", result_lpa_n1, data.getSceneInstId(), null);
//                    }
//                }
                //总销售价计算值
                if(!KiteStringUtils.isEmpty(sa_n1)  && !KiteStringUtils.isEmpty(orderYear)){
                    String result_sa_n1 = OrderModelCalculateHelper.getResultSa(sa_n1,orderYear);
                    if(KiteStringUtils.isNotEmpty(result_sa_n1)) {
                        operateInstAttrService.setAttrValue(
                                inst, "210008140", result_sa_n1, data.getSceneInstId(), null);
                    }
                }
            }
            //订购模式为包年服务N2档次
            if (KiteStringUtils.isEqual(attrValue, "110015094")) {
                //结算价折扣率清空
                if(!KiteStringUtils.isBlank(discountRate)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008073", null, data.getSceneInstId(), null);
                }
                //初始化N2销售价为默认值
                operateInstAttrService.setAttrValue(
                        inst, "210008083", "3.5", data.getSceneInstId(), null);
                LastestPriceAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                year.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                //必填
                year.setIsNull(KeyConsts.IFTRUE_F);
//                //总结算价计算值
//                if(!KiteStringUtils.isEmpty(lpa_n2) && !KiteStringUtils.isEmpty(orderYear)){
//                    String result_lpa_n2 = OrderModelCalculateHelper.getResultLpa(lpa_n2,discountRate,orderYear);
//                    if(KiteStringUtils.isNotEmpty(result_lpa_n2)) {
//                        operateInstAttrService.setAttrValue(
//                                inst, "210008139", result_lpa_n2, data.getSceneInstId(), null);
//                    }
//                }
                //总销售价计算值
                if(!KiteStringUtils.isEmpty(sa_n2)  && !KiteStringUtils.isEmpty(orderYear)){
                    String result_sa_n2 = OrderModelCalculateHelper.getResultSa(sa_n2,orderYear);
                    if(KiteStringUtils.isNotEmpty(result_sa_n2)) {
                        operateInstAttrService.setAttrValue(
                                inst, "210008140", result_sa_n2, data.getSceneInstId(), null);
                    }
                }
            }
            //订购模式为包年服务N3档次
            if (KiteStringUtils.isEqual(attrValue, "110015095")) {
                //结算价折扣率清空
                if(!KiteStringUtils.isBlank(discountRate)) {
                    operateInstAttrService.setAttrValue(
                            inst, "210008073", null, data.getSceneInstId(), null);
                }
                //初始化N3销售价为默认值
                operateInstAttrService.setAttrValue(
                        inst, "210008085", "4.5", data.getSceneInstId(), null);
                LastestPriceAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_D3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N2.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                sellAttr_N1.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                LastestPriceAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_N3.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                year.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LastestPriceAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                sellAttr_all.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                //必填
                year.setIsNull(KeyConsts.IFTRUE_F);
//                //总结算价计算值
//                if(!KiteStringUtils.isEmpty(lpa_n3) && !KiteStringUtils.isEmpty(orderYear)){
//                    String result_lpa_n3 = OrderModelCalculateHelper.getResultLpa(lpa_n3,discountRate,orderYear);
//                    if(KiteStringUtils.isNotEmpty(result_lpa_n3)) {
//                        operateInstAttrService.setAttrValue(
//                                inst, "210008139", result_lpa_n3, data.getSceneInstId(), null);
//                    }
//                }
                //总销售价计算值
                if(!KiteStringUtils.isEmpty(sa_n3)  && !KiteStringUtils.isEmpty(orderYear)){
                    String result_sa_n3 = OrderModelCalculateHelper.getResultSa(sa_n3,orderYear);
                    if(KiteStringUtils.isNotEmpty(result_sa_n3)) {
                        operateInstAttrService.setAttrValue(
                                inst, "210008140", result_sa_n3, data.getSceneInstId(), null);
                    }
                }
            }
            OrderModelCalculateHelper.calculateSettlePrice(attr, inst, data);
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
